<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
	Name	: Master class controller
	Author	: Iswan Putera S.Kom
	Date	: 01/05/2013
*/

class Master extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->library('zetro_auth');
		$this->load->model('main_model');
		$this->zetro_auth->check_login();
		//force_ssl();
	}
	

	//pengaturan profile
	function profile(){
		$data['tab_header']=$this->zetro_form->generate_tab_header('Profile');
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/zetro_user.frm');
		$this->zetro_form->section('profile');
		$this->zetro_auth->menus('profile');
		//$this->zetro_form->generate_dropdown_content('lokasi_tp','nm_lokasi_tp','nm_lokasi_tp');
		$this->zetro_form->addButtons(false);
		(seriale()=='Demo')?
		$this->zetro_form->buttons(false):
		$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?true:false);
		$data['form']=$this->zetro_form->frm_std();
		$data['serial']=seriale();
		$this->load->view('admin/header');
		$this->load->view('admin/master_app',$data);
		$this->load->view('admin/footer');
	}
	function get_profile(){
		$data=array();
		$ID=$_POST['id'];
		$file='asset/frm/zetro_config.dll';
		$data=array(
			  'Name'	=>base64_decode($this->zetro_manager->rContent($ID,'Nama',$file)),
			  'Address' =>base64_decode($this->zetro_manager->rContent($ID,'Address',$file)),
			  'Kota'	=>base64_decode($this->zetro_manager->rContent($ID,'City',$file)),
			  'Head'	=>base64_decode($this->zetro_manager->rContent($ID,'BH',$file)),
			  'BU'	  =>base64_decode($this->zetro_manager->rContent($ID,'BussinesType',$file)),
			  'Telp'	=>base64_decode($this->zetro_manager->rContent($ID,'Phone',$file)),
			  'AccHead' =>base64_decode($this->zetro_manager->rContent($ID,'AccHead',$file)),
			  'SerNo'   =>seriale()
			  );
		echo json_encode($data);		
	}
	
	function set_profile(){
		$file='asset/frm/zetro_config.dll';
		$this->zetro_manager->add_content($file,'company','Nama',base64_encode($_POST['Name']));
		$this->zetro_manager->add_content($file,'company','Address',base64_encode($_POST['Address']));
		$this->zetro_manager->add_content($file,'company','City',base64_encode($_POST['Kota']));
		$this->zetro_manager->add_content($file,'company','BussinesType',base64_encode($_POST['BU']));
		$this->zetro_manager->add_content($file,'company','BH',base64_encode($_POST['Head']));
		$this->zetro_manager->add_content($file,'company','Phone',base64_encode($_POST['Telp']));
		//$this->zetro_manager->add_content($file,'company','AccHead',base64_encode($_POST['AccHead']));
		echo "Data sudah berhasil di simpan";
	}
	//backup database
	function backup()
	{
		$data['tab_header']=$this->zetro_form->generate_tab_header('Backup Database');
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/zetro_user.frm');
		$this->zetro_form->section('backupdb');
		$this->zetro_auth->menus('backup_database');
		$this->zetro_form->addButtons(false);
		$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?false:false);
		$data['form']=$this->zetro_form->frm_std();
		$this->load->view('admin/header');
		$this->load->view('admin/master_backup',$data);
		$this->load->view('admin/footer');
	}
	function lokasi()
	{
		$data=array();
		//menu header
		$data['tab_header']=$this->zetro_form->generate_tab_header('Data Lokasi');
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/zetro_master.frm');
		$this->zetro_form->section('lokasi');
		$this->zetro_auth->menus('data_lokasi');
		$this->zetro_form->generate_dropdown_content('lokasi_tp','nm_lokasi_tp','nm_lokasi_tp');
		$this->zetro_form->buttons(true);
		$data['form']=$this->zetro_form->frm_std();
		$this->zetro_auth->menus('data_lokasi');
		$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?true:false);
		$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm2'),"Edit Lokasi");
		//generate list data lokasi
		$content='';//$this->get_list_lokasi();
		$this->zetro_form->section('listLokasi');
		$this->zetro_form->action_col(true);
		$content.="";//$this->zetro_form->DataNotFound($lst_usr,4);
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std();
		//send data to view
		$this->load->view('admin/header');
		$this->load->view('administration/lokasi',$data);
		$this->load->view('admin/footer');
	}
	//add data to table lokasi_tp
	function set_tp_lokasi()
	{
		$data['nm_lokasi_tp']=addslashes($_POST['nm']);
		$this->main_model->table_name('lokasi_tp');
		$this->main_model->set_methods('replace');
		$this->main_model->get_content($data);
		$this->main_model->set_data();
		echo $this->zetro_form->generate_dropdown_content('lokasi_tp','nm_lokasi_tp','nm_lokasi_tp');//:'';
			
	}
	
	function set_lokasi()
	{
		$data['tp_lokasi']	=empty($_POST['tp_lokasi'])?'':$_POST['tp_lokasi'];
		$data['id_lokasi']	=empty($_POST['id_lokasi'])?'0':$_POST['id_lokasi'];
		$data['nm_lokasi']	=empty($_POST['nm_lokasi'])?'':$_POST['nm_lokasi'];
		$data['add_lokasi']	=empty($_POST['add_lokasi'])?'':$_POST['add_lokasi'];
		$data['telp_lokasi']=empty($_POST['telp_lokasi'])?'':$_POST['telp_lokasi'];
		$data['created_by']	=$this->session->userdata('username');
		$this->main_model->prepared_data('lokasi',$data);
		echo success($this->main_model->set_data());
	}
	function get_detail_lokasi()
	{
		$data=array();
		$id=$_POST['id'];
		$this->main_model->table_name('lokasi');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('id_lokasi'=>$id));
		$data=$this->main_model->set_data();
		echo json_encode($data[0]);	
	}
	function get_list_lokasi()
	{
		$content='';$i=0;$lst_usr=array();
		$tp=empty($_POST['tp'])?'':$_POST['tp'];
		$ordby=empty($_POST['ordby'])?'id_lokasi':$_POST['ordby'];
		$this->main_model->table_name('lokasi');
		$this->main_model->query();
		$this->main_model->orderby($ordby);
		$this->main_model->set_methods('select_ordered');
		$lst_usr=$this->main_model->set_data();
		foreach($lst_usr as $r)
		{
			$i++;
			$this->zetro_auth->menus('data_lokasi');$cek='0';
			$cek=$this->main_model->get_db('member',array("id_lokasi"=>$r->id_lokasi),'id_lokasi');
			$icon=($this->zetro_auth->cek_oto('e')!='')?
				  $this->zetro_form->z_icon($r->id_lokasi,array('cus-page-white-edit',($cek!='0')?'cus-':'cus-cross'),array('edit',($cek!='0')?'':'deleted')):'';
			$content.=tr().td($i,false,'','','center').
					  td($r->tp_lokasi).td(str_repeat('0',(4-strlen($r->id_lokasi))).$r->id_lokasi).td($r->nm_lokasi).
					  td($r->add_lokasi).td($r->telp_lokasi).
					  td($icon,false,'','','center')._tr();
		}
			$content.=$this->zetro_form->DataNotFound($lst_usr,7);
			if($tp=='y'){echo $content;}else{ return $content;}
	}
	
	function deleted_lokasi()
	{
		$id=$_POST['id'];
		$this->main_model->table_name('lokasi');
		$this->main_model->set_methods('delete');
		$this->main_model->where(array('id_lokasi'=>$id));
		$this->main_model->set_data();	
	}
// jenis simpanan
	function jenis_simpanan()
	{
		$data=array();
		//menu header
		$data['tab_header']=$this->zetro_form->generate_tab_header('Kode Simpanan');
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/zetro_master.frm');
		$this->zetro_auth->menus('add_simpanan');
		$this->zetro_form->section('jsimpanan');
		$this->zetro_form->get_select_opt();
		$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?true:false);
		$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm2'),"Add Simpanan");
		//generate list data lokasi
		$content='';//$this->get_list_lokasi();
		$this->zetro_form->section('listsimpanan');
		$this->zetro_form->action_col(true);
		$content.="";//$this->zetro_form->DataNotFound($lst_usr,4);
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std('NewList','65%');
		//send data to view
		$this->load->view('admin/header');
		$this->load->view('administration/simpanan',$data);
		$this->load->view('admin/footer');
	}
	function set_jenis_simpanan()
	{
		$data=array();
		$akun=explode(":",$_POST['kd_akun']);
		$data['id_simpanan']=empty($_POST['id_simpanan'])?'0':$_POST['id_simpanan'];
		$data['nm_simpanan']=empty($_POST['nm_simpanan'])?'':addslashes($_POST['nm_simpanan']);
		$data['id_calc']	=empty($_POST['id_calc'])?'1':$_POST['id_calc'];
		$data['id_akun']	=empty($_POST['kd_akun'])?'':$akun[0];
		$data['created_by']	=$this->session->userdata('username');
		$this->main_model->prepared_data('simpanan_jenis',$data);
		echo success($this->main_model->set_data());
	}
	function get_jenis_simpanan()
	{
		$content='';$i=0;$lst_usr=array();
		$ordby=empty($_POST['ordby'])?'id_simpanan':$_POST['ordby'];
		$this->main_model->table_name('simpanan_jenis_v');
		$this->main_model->query();
		$this->main_model->orderby($ordby);
		$this->main_model->set_methods('select_ordered');
		$lst_usr=$this->main_model->set_data();
			foreach($lst_usr as $r)
			{
				$i++;
				$this->zetro_auth->menus('kode_simpanan');
				$icon=($this->zetro_auth->cek_oto('e')!='')?
					  $this->zetro_form->z_icon($r->id_simpanan,array('cus-page-white-edit','cus-cross'),array('edit','deleted')):'';
				$content.=tr().td($i,false,'','','center').
						  td($r->id_akun,false,'','','center').
						  td($r->nm_simpanan).
						  td($r->j_calc).
						  td($icon,false,'','','center').
						  _tr();
			}
			$content.=$this->zetro_form->DataNotFound($lst_usr,7);
		echo $content;
	}
	function get_detail_jsim()
	{
		$data=array();
		$id=$_POST['id'];
		$this->main_model->table_name('simpanan_jenis_v');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('id_simpanan'=>$id));
		$data=$this->main_model->set_data();
		echo json_encode($data[0]);	
			
	}
	function del_simpanan()
	{
		$id=$_POST['id'];
		$this->main_model->table_name('simpanan_jenis');
		$this->main_model->set_methods('delete');
		$this->main_model->where(array('id_simpanan'=>$id));
		$this->main_model->set_data();	
	}
//setting neraca
	function neraca()
	{
		$data['tab_header']=$this->zetro_form->generate_tab_header('Kode Neraca');
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/zetro_master.frm');
		$this->zetro_auth->menus('kode_neraca');
		$this->zetro_form->section('setneraca');
		$this->zetro_form->get_select_opt();
		$this->zetro_form->tp_ahead($this->get_akun(array('cid_parent <' =>'410')));
		$this->zetro_form->AddFields("<input type='hidden' id='id_neraca' name='id_neraca' value=''/>");
		$this->zetro_form->Modale('frm2');
		$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?true:false);
		$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm2'),'Add Kode Rekening to Neraca');
		//generate list data lokasi
		$content='';//$this->get_list_lokasi();
		$this->zetro_form->section('listsetneraca');
		$this->zetro_form->action_col(true);
		$content.="";//$this->zetro_form->DataNotFound($lst_usr,4);
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std();
		//send data to view
		$this->load->view('admin/header');
		$this->load->view('administration/set_neraca',$data);
		$this->load->view('admin/footer');
	}
	
	function get_akun($where='')
	{
		$data=array();$akuns="";
		$this->main_model->where($where);
		$this->main_model->table_name('v_akun_display');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		//$akuns='[';
		foreach($data as $r)
		{
			$akuns.="&quot;".$r->kodeakun."&quot;,";
		}
		//$akuns.=']';
		return substr($akuns,0,-1);
	}
	function get_akunes($where='')
	{
		$data=array();$akuns=array();
		$this->main_model->where($where);
		$this->main_model->table_name('v_akun_display');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		//$akuns='[';
		foreach($data as $r)
		{
			$akuns[]=$r->kodeakun;
		}
		//$akuns.=']';
		echo json_encode($akuns);
	}
	function set_neraca()
	{
		$data=array();
		$akun=explode(':',$_POST['kd_akun']);
		$data['id']		 =empty($_POST['id_neraca'])?'0':$_POST['id_neraca'];
		$data['kd_akun']	=empty($_POST['kd_akun'])?$_POST['kd_neraca']:$akun[0];
		$data['nm_neraca']  =$_POST['nm_neraca'];
		$data['kd_neraca']  =$_POST['kd_neraca'];
		$data['created_by'] =$this->session->userdata('username');
		$this->main_model->prepared_data('akun_to_neraca',$data);
		echo success($this->main_model->set_data());
	}
	function get_list_neraca()
	{
			$content='';$n=0;
			$ordby=empty($_POST['ordby'])?'cid_parent':$_POST['ordby'];
			$this->main_model->table_name('v_akun_neraca');
			$this->main_model->query();
			//$this->main_model->orderby($ordby);
			$this->main_model->set_methods('select');
			$lst_usr=$this->main_model->set_data();
			foreach($lst_usr as $r)
			{
				$n=(strlen($r->kd_akun)==3)?($n+1):$n;$jml=1;
				$jml=$this->main_model->countRecord('v_akun_neraca',array("kd_neraca"=>$r->kd_neraca));
				if($jml==1 ){$hps='cus-delete';$hpsa='del';}
				else{$hps=(strlen($r->kd_akun) ==3)?'cus-':'cus-cross';$hpsa=(strlen($r->kd_akun) ==3)?'':'del';}
				$this->zetro_auth->menus('kode_neraca');
				$icon=($this->zetro_auth->cek_oto('e')!='')?
					  $this->zetro_form->z_icon($r->id.":".$r->kd_neraca,
					  		array((strlen($r->kd_akun)==3)?'cus-add':'cus-',(strlen($r->kd_akun) ==3)?'cus-page-white-edit':'cus-',$hps),
							array((strlen($r->kd_akun)==3)?'add':'',(strlen($r->kd_akun)==3)?'edit':'',$hpsa)):'';
				$content.=tr((strlen($r->kd_akun) ==3)?'xx alert tree-toggler\' id=\''.$r->kd_neraca.'':'xx tree\' id=\'t_'.$r->kd_neraca.'\' style=\'display:none').
						  td((strlen($r->kd_akun)==3)?$n:'',false,'','','center').
						  td((strlen($r->kd_akun)==3)?$r->kd_akun:nbs(5).$r->kd_akun,false,'','','left').
						  td((strlen($r->kd_akun)==3)?$r->nm_neraca:$r->cnama).
						  td($icon,false,'','','center').
						  _tr();
				
			}
			
			$content.=$this->zetro_form->DataNotFound($lst_usr,4);
		echo $content;
	}
	function get_detail_neraca()
	{
		$data=array();
		$id=empty($_POST['id'])?'':$_POST['id'];$kd=$_POST['kd'];
		$this->main_model->table_name('v_akun_neraca');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		empty($_POST['id'])?$this->main_model->where(array('kd_neraca'=>$kd)):
		$this->main_model->where(array('id'=>$id,'kd_neraca'=>$kd));
		$data=$this->main_model->set_data();
		echo json_encode($data[0]);	
	}
	
	function del_neraca()
	{
		$id=$_POST['id'];
		$this->main_model->table_name('akun_to_neraca');
		$this->main_model->set_methods('delete');
		$this->main_model->where(array('id'=>$id));
		$this->main_model->set_data();	
	}
//setting laba rugi
	function labarugi()
	{
		$data['tab_header']=$this->zetro_form->generate_tab_header('Kode Neraca');
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/zetro_master.frm');
		$this->zetro_auth->menus('setting_laba_rugi');
		$this->zetro_form->section('setlr');
		$this->zetro_form->get_select_opt();
		$this->zetro_form->tp_ahead($this->get_akun(array('cid_parent >=' =>'410')));
		$this->zetro_form->AddFields("<input type='hidden' id='id_neraca' name='id_neraca' value=''/>");
		$this->zetro_form->Modale('frm2');
		$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?true:false);
		$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm2'),'Add Kode Rekening to Laba Rugi');
		//generate list data lokasi
		$content='';//$this->get_list_lokasi();
		$this->zetro_form->section('listsetlr');
		$this->zetro_form->action_col(true);
		$content.="";//$this->zetro_form->DataNotFound($lst_usr,4);
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std();
		//send data to view
		$this->load->view('admin/header');
		$this->load->view('administration/set_labarugi',$data);
		$this->load->view('admin/footer');
	}
	function set_labarugi()
	{
		$data=array();
		$akun=explode(':',$_POST['kd_akun']);
		$data['id']		 =empty($_POST['id_neraca'])?'0':$_POST['id_neraca'];
		$data['kd_akun']	=empty($_POST['kd_akun'])?$_POST['kd_neraca']:$akun[0];
		$data['nm_labarugi']  =$_POST['nm_labarugi'];
		$data['kd_labarugi']  =$_POST['kd_labarugi'];
		$data['created_by'] =$this->session->userdata('username');
		$this->main_model->prepared_data('akun_to_labarugi',$data);
		echo success($this->main_model->set_data());
	}
	function get_detail_labarugi()
	{
		$data=array();
		$id=empty($_POST['id'])?'':$_POST['id'];$kd=$_POST['kd'];
		$this->main_model->table_name('v_akun_labarugi');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		empty($_POST['id'])?$this->main_model->where(array('kd_labarugi'=>$kd)):
		$this->main_model->where(array('id'=>$id,'kd_labarugi'=>$kd));
		$data=$this->main_model->set_data();
		echo json_encode($data[0]);	
	}
	function get_list_labarugi()
	{
			$content='';$n=0;
			$ordby=empty($_POST['ordby'])?'cid_parent':$_POST['ordby'];
			$this->main_model->table_name('v_akun_labarugi');
			$this->main_model->query();
			//$this->main_model->orderby($ordby);
			$this->main_model->set_methods('select');
			$lst_usr=$this->main_model->set_data();
			foreach($lst_usr as $r)
			{
				$n=(strlen($r->kd_akun)==3)?($n+1):$n;$jml=1;
				$jml=$this->main_model->countRecord('v_akun_labarugi',array("kd_labarugi"=>$r->kd_labarugi));
				if($jml==1 ){$hps='cus-delete';$hpsa='del';}
				else{$hps=(strlen($r->kd_akun) ==3)?'cus-':'cus-cross';$hpsa=(strlen($r->kd_akun) ==3)?'':'del';}
				$this->zetro_auth->menus('setting_laba_rugi');
				$icon=($this->zetro_auth->cek_oto('e')!='')?
					  $this->zetro_form->z_icon($r->id.":".$r->kd_labarugi,
					  		array((strlen($r->kd_akun)==3)?'cus-add':'cus-',(strlen($r->kd_akun) ==3)?'cus-page-white-edit':'cus-',$hps),
							array((strlen($r->kd_akun)==3)?'add':'',(strlen($r->kd_akun)==3)?'edit':'',$hpsa)):'';
				$content.=tr((strlen($r->kd_akun)==3)?'xx alert tree-toggler\' id=\''.$r->kd_labarugi.'':'xx tree\' id=\'t_'.$r->kd_labarugi.'\' style=\'display:none').
						  td((strlen($r->kd_akun)==3)?$n:'',false,'','','center').
						  td((strlen($r->kd_akun)==3)?$r->kd_akun:nbs(5).$r->kd_akun,false,'','','left').
						  td((strlen($r->kd_akun)==3)?$r->nm_labarugi:$r->cnama).
						  td($icon,false,'','','center').
						  _tr();
				
			}
			
			$content.=$this->zetro_form->DataNotFound($lst_usr,4);
		echo $content;
	}
	
	function del_labarugi()
	{
		$id=$_POST['id'];
		$this->main_model->table_name('akun_to_labarugi');
		$this->main_model->set_methods('delete');
		$this->main_model->where(array('id'=>$id));
		$this->main_model->set_data();	
	}
	
//end of master class	
}
